package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyHuiQianKun;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeHuiQianKun;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 * 安心分期-汇乾锟API-山东60
 * @ Author 江小游
 * @ Date 2025/5/13 15:40
 */
@ApiPlan(productId = "193", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5HuiQianKun implements ApiOrgPhoneMd5ConvertHandler {
    private static final String PUBLIC_KEY = "5whCyl2qakLeNtVq5T3MFH3ybhqmBAIODZ2om0eDyNxr7Xx1s5";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        // 表单类型
        apiDataExchangeParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        // 创建实体类
        OrgDataExchangeHuiQianKun anXinFenQiShenzhenData = new OrgDataExchangeHuiQianKun();
        anXinFenQiShenzhenData.setEnmobile(apiDataExchangeParameter.getPhoneMd5());
        anXinFenQiShenzhenData.setKey(PUBLIC_KEY);
        return anXinFenQiShenzhenData;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("errNum"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        apiApplyParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        // 创建进件实体类
        OrgApplyHuiQianKun orgApplyHuiQianKun = new OrgApplyHuiQianKun();
        // 创建拓展工具类
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        // 渠道名称
        orgApplyHuiQianKun.setChannel("安心分期");
        // 客户姓名
        orgApplyHuiQianKun.setName(apiApplyParameter.getUserName());
        // 手机号
        orgApplyHuiQianKun.setMobile(apiApplyParameter.getPhone());
        // 密钥
        orgApplyHuiQianKun.setKey(PUBLIC_KEY);
        // 城市（上海、北京……） 不带市
        orgApplyHuiQianKun.setCity(apiApplyParameter.getCity().replace("市", ""));
        // 需求金额（元）
        orgApplyHuiQianKun.setMoney(Float.valueOf(apiFieldConverter.getLoanAmountInt()));
        // 性别（男/女）
        orgApplyHuiQianKun.setSex(apiFieldConverter.getSexText());
        // 年龄
        orgApplyHuiQianKun.setAge(apiFieldConverter.getAge());
        // 学历
        orgApplyHuiQianKun.setXueli(apiFieldConverter.getEducationAttainmentText());
        // 是否有车
        String carInfo = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", "无")
                .when(apiApplyParameter.getCarInfo(), "2", "有")
                .when(apiApplyParameter.getCarInfo(), "3", "有")
                .when(apiApplyParameter.getCarInfo(), "4", "有")
                .end("无");
        orgApplyHuiQianKun.setHas_car(carInfo);
        // 是否有房
        String realEstateInfo = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", "无")
                .when(apiApplyParameter.getRealEstateInfo(), "2", "有")
                .when(apiApplyParameter.getRealEstateInfo(), "3", "有")
                .when(apiApplyParameter.getRealEstateInfo(), "4", "有")
                .when(apiApplyParameter.getRealEstateInfo(), "5", "有")
                .when(apiApplyParameter.getRealEstateInfo(), "6", "有")
                .end("无");
        orgApplyHuiQianKun.setHas_house(realEstateInfo);
        // 是否有公积金
        String providentFund = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", "无")
                .when(apiApplyParameter.getProvidentFund(), "2", "有")
                .when(apiApplyParameter.getProvidentFund(), "3", "有")
                .when(apiApplyParameter.getProvidentFund(), "4", "有")
                .end("无");
        orgApplyHuiQianKun.setHas_gongjijin(providentFund);
        // 是否有表单
        String insurancePolicy = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", "无")
                .when(apiApplyParameter.getInsurancePolicy(), "2", "有")
                .when(apiApplyParameter.getInsurancePolicy(), "3", "有")
                .when(apiApplyParameter.getInsurancePolicy(), "4", "有")
                .end("无");
        orgApplyHuiQianKun.setHas_baodan(insurancePolicy);
        // 是否有社保
        String socialSecurity = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", "无")
                .when(apiApplyParameter.getSocialSecurity(), "2", "有")
                .when(apiApplyParameter.getSocialSecurity(), "3", "有")
                .when(apiApplyParameter.getSocialSecurity(), "4", "有")
                .end("无");
        orgApplyHuiQianKun.setHas_shebao(socialSecurity);
        // 是否有营业执照
        String bizLicenseYearToStr = ChooseEq.create()
                .when(apiApplyParameter.getBizLicenseYear(), "1", "无")
                .when(apiApplyParameter.getBizLicenseYear(), "2", "有")
                .when(apiApplyParameter.getBizLicenseYear(), "3", "有")
                .when(apiApplyParameter.getBizLicenseYear(), "4", "有")
                .when(apiApplyParameter.getBizLicenseYear(), "5", "有")
                .end("无");
        orgApplyHuiQianKun.setHas_company(bizLicenseYearToStr);
        // 是否有备注
        orgApplyHuiQianKun.setRemark(apiApplyParameter.getFormRemark());
        return orgApplyHuiQianKun;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("errNum"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
